package com.web.controller;

import com.common.utils.Page;
import com.entity.*;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String userid,String password, Model model,
						HttpSession session) {
		// 通过账号和密码查询用户
		User userc = userService.findUserid(userid);
		User user = userService.findUser(userid, password);
		if(userc!=null) {
			if(user != null){
				// 将用户对象添加到Session
				session.setAttribute("USER_SESSION", user);
				// 跳转到主页面
				return "user/index";
			}
			session.setAttribute("msg","账号或密码错误，请重新输入");
			//model.addAttribute("msg", "账号或密码错误，请重新输入！");
			// 返回到登录页面
			return "login";
		}else {
			model.addAttribute("msg", "账号"+userid+"不存在，请注册！");
			return "register";
		}
	}
	/**
	 * 用户注册
	 */
	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	public String register(String tel,String username,String password, Model model,
						   HttpSession session) {
		String maxID=userService.maxID();
		if(maxID.length()<=10) {
			int a = Integer.parseInt(maxID);
			a=a+1;
			maxID=Integer.toString(a);
		}
		String  tel1 = userService.checktel(tel);
		if(tel1!=null&&tel1!="") {
			model.addAttribute("msg", "注册失败，电话已注册账号："+tel);
			return "register";
		}else {
			userService.addUser(tel, password, username,maxID);
			model.addAttribute("msg", "注册成功，请登录。");
		}
		return "login";
	}
	@RequestMapping(value = "/user/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,String username, Model model,
			HttpServletRequest request,HttpSession session) {
		// 条件查询所有客户
		username=request.getParameter("username");
		if(username==null) username="";
		String username1=request.getParameter("username1");
		if(username1==null) username1="0";
		if(username1.equals("1")) {
			String str=(String) session.getAttribute("username");
			if(str!=null&&str!="") username=str;
		}
		Page<User> users = userService
				.findUserList(page, rows,username);
		model.addAttribute("page", users);
		model.addAttribute("username", username);
		return "admin/user";
	}

	/**
	 * 通过id获取客户信息
	 */
	@RequestMapping("/user/getUserById.action")
	@ResponseBody
	public User getUserById(Integer id) {
	    User user = userService.getUserById(id);
	    return user;
	}
	
	/**
	 * 创建客户
	 */
	@RequestMapping("/user/create.action")
	@ResponseBody
	public String userCreate(User user) {
		String maxID=userService.maxID();
		System.out.println(user.getTel());
		if(user.getUsername()==""||user.getUsername()==null) {
			return "uname";
		}
		if(user.getPassword()==""||user.getPassword()==null) {
			return "paw";
		}
		if(user.getTel()==""||user.getTel()==null) {
			return "tel";
		}
		String  tel1 = userService.checktel(user.getTel());
		if(tel1!=null&&tel1!="") {
			return "tels";
		}
		if(maxID.length()<=10) {
			int a = Integer.parseInt(maxID);
			a=a+1;
			maxID=Integer.toString(a);
			user.setUserid(maxID);
		}
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = userService.createUser(user);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 更新客户
	 */
	@RequestMapping("/user/updateUser.action")
	@ResponseBody
	public String userUpdate(User user) {
		if(user.getUsername()==""||user.getUsername()==null) {
			System.out.println(user.getUsername());
			return "uname";
		}
		if(user.getTel()==""||user.getTel()==null) {
			System.out.println(user.getUsername());
			return "tel";
		}
		Integer id=user.getId();
		User user1 = userService.getUserById(id);
		String  tel1 = userService.checktel(user.getTel());
		if(tel1!=null&&tel1!=""&&!user1.getTel().equals(user.getTel())) {
			return "tels";
		}
		int rows = userService.updateUser(user);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	@RequestMapping("/user/update.action")
	@ResponseBody
	public String Update(User user) {
		//Integer id=user.getId();
		//User userpaw=userService.getUserById(id);
		if(user.getUsername()==""||user.getUsername()==null) {
			System.out.println(user.getUsername());
			return "uname";
		}
		if(user.getTel()==""||user.getTel()==null) {
			System.out.println(user.getUsername());
			return "tel";
		}
		Integer id=user.getId();
		User user1 = userService.getUserById(id);
		String  tel1 = userService.checktel(user.getTel());
		if(tel1!=null&&tel1!=""&&!user1.getTel().equals(user.getTel())) {
			return "tels";
		}
		if(user.getPassword()!=""&&user.getPassword()!=null) {
			if(!user.getPassword().equals(user1.getPassword())) {
				return "paw";
			}
		}else {
			user.setNewpassword(null);
		}
		int rows = userService.updateUser(user);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	/**
	 * 删除客户
	 */
	@RequestMapping("/user/delete.action")
	@ResponseBody
	public String userDelete(Integer id) {
	    int rows = userService.deleteUser(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}
	@RequestMapping(value = "/register.action", method = RequestMethod.GET)
	public String toRegister() {
		return "register";
	}
	@RequestMapping(value = "/user/index.action", method = RequestMethod.GET)
	public String toIndex() {
		return "user/index";
	}
	@RequestMapping(value = "/user/information.action", method = RequestMethod.GET)
	public String toInformation() {
		return "user/information";
	}
	@RequestMapping(value = "/user/c_information.action", method = RequestMethod.GET)
	public String tocInformation(Model model,@RequestParam(defaultValue="1")Integer pages
			,@RequestParam(defaultValue="")String title,@RequestParam(defaultValue="0")Integer id) {
		model.addAttribute("title1", "1");
		model.addAttribute("pages", pages);
		model.addAttribute("id", id);
		return "user/c_information";
	}
	@RequestMapping(value = "/user/i_losearticle.action", method = RequestMethod.GET)
	public String toiLosearticle(Model model,@RequestParam(defaultValue="1")Integer pages
			,@RequestParam(defaultValue="")String title,@RequestParam(defaultValue="0")Integer id) {
		model.addAttribute("goods1", "1");
		model.addAttribute("pages", pages);
		model.addAttribute("id", id);
		return "user/i_loserartilce";
	}
	@RequestMapping(value = "/user/c_losearticle.action", method = RequestMethod.GET)
	public String tocLosearticle(Model model,@RequestParam(defaultValue="1")Integer pages
			,@RequestParam(defaultValue="")String title,@RequestParam(defaultValue="0")Integer id) {
		model.addAttribute("goods1", "1");
		model.addAttribute("pages", pages);
		model.addAttribute("id", id);
		return "user/c_loserartilce";
	}
}
